package com.ktwlsoft.fundmanage.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ktwlsoft.fundmanage.payload.IAccountUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户工具类
 *
 * @author zhuhe on 2019/3/26 002614:45
 * @version 1.0
 */
public class UserUtils {

    private static Logger logger = LoggerFactory.getLogger(UserUtils.class);

    /**
     * 获取用户 on 2019/3/26 0026 15:49
     * 
     * @return IAccountUser
     */
    public static IAccountUser getUser() {
        //获取token
        String token = null;
        try {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            token = request.getHeader("token");
        }catch (Exception e){
            logger.error(e.getMessage());
            return JSON.toJavaObject(new JSONObject(), IAccountUser.class);
        }
        if(token == null){
            return JSON.toJavaObject(new JSONObject(), IAccountUser.class);
        }
        //请求数据
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token", token);
        String s = HttpPoolUtils.get(BasicsPlatformUrl.getGetByToken(), headers, null);
        //转string数据为json
        JSONObject object = JSONObject.parseObject(s);
        if(object == null || object.get("data") == null || !object.getBoolean("success")) {
            return JSONObject.parseObject(s,IAccountUser.class);
        }
        //转json数据为Javabean
        return JSON.toJavaObject(object.getJSONObject("data"), IAccountUser.class);
    }

    public static String getToken(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        return request.getHeader("innerToken");
    }

    public static List<String> getRoles(){
        //获取token
        String token = null;
        try {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            token = request.getHeader("token");
        }catch (Exception e){
            logger.error(e.getMessage());
            return null;
        }
        if(token == null){
            return null;
        }
        //请求数据
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token", token);
        String s = HttpPoolUtils.get(BasicsPlatformUrl.getGetByToken(), headers, null);
        JSONObject object = JSONObject.parseObject(s);
        List<String> roles = new ArrayList<>();
        if(object!=null){
            JSONArray rolesObjects = object.getJSONObject("data").getJSONArray("roleInfos");
            for(int i=0;i<rolesObjects.size();i++){
                JSONObject roleObj = rolesObjects.getJSONObject(i);
                roles.add(roleObj.getString("name"));
            }
        }
        return roles;
    }

    /**
      * 通过id获取用户 on 2019/4/9 15:36
      *
      * @param id id
      * @return IAccountUser
      */
    public static IAccountUser getById(String id){
        //获取token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        //请求头
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token", token);
        //请求数据
        String s = HttpPoolUtils.get(BasicsPlatformUrl.getGetById() + id + "/pvs/open", headers, null);
        //转string数据为json
        JSONObject object = JSONObject.parseObject(s);
        if(object == null || object.get("data") == null || !object.getBoolean("success")) {
            return JSONObject.parseObject(s,IAccountUser.class);
        }
        //转json数据为Javabean
        return JSON.toJavaObject(object.getJSONObject("data"), IAccountUser.class);
    }

    /**
      * 更新手机号 on 2019/5/17 16:11
      *
      * @param userId userId
      * @param phone phone
      * @return boolean
      */
    public static JSONObject updatePhone(String userId,String phone){
        //获取token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        //请求头
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token", token);
        //参数
        Map<String,String> params = Maps.newHashMap();
        params.put("userId",userId);
        params.put("mobile",phone);
        String s = HttpPoolUtils.put(BasicsPlatformUrl.getGetById(), headers, params);
        //转string数据为json
        return JSONObject.parseObject(s);
    }

    /**
     * 更新邮箱 on 2019/5/17 16:11
     *
     * @param userId userId
     * @param mail mail
     * @return boolean
     */
    public static JSONObject updateMail(String userId,String mail){
        //获取token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        //请求头
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token", token);
        //参数
        Map<String,String> params = Maps.newHashMap();
        params.put("userId",userId);
        params.put("email",mail);
        String s = HttpPoolUtils.put(BasicsPlatformUrl.getGetById(), headers, params);
        //转string数据为json
        return JSONObject.parseObject(s);
    }

    /**
     *
     * 毕节房地产市场监管平台
     *
     * */

    /**
      * 二手房资金监管获取网签信息 on 2019/10/16 15:30
      *
      * @param signingCode 网签编号
      * @return JSONObject JSONObject
      */
    public static JSONObject getShhSign(String signingCode){
        //参数
        Map<String,String> params = Maps.newHashMap();
        params.put("signingCode",signingCode);
        String s = HttpPoolUtils.get(BasicsPlatformUrl.getGetShhSign(), null, params);
        //转string数据为json
        JSONObject object = JSONObject.parseObject(s);
        if(object == null || object.get("data") == null || !object.getBoolean("success")) {
            return JSONObject.parseObject(s);
        }
        //转json数据为Javabean
        return JSON.toJavaObject(object.getJSONObject("data"),JSONObject.class);
    }

    /**
     * 二手房资金监管获取网签信息 on 2019/10/16 15:30
     *
     * @param busiType 业务类型 10=商品房抵押， 20=二手房交易
     * @param signingStatus 网签状态，不传返回所有变化的数据
     * @param startTime 网签状态变化开始时间
     * @param endTime 网签状态变化结束时间
     * @return JSONObject JSONObject
     */
    public static List<JSONObject> getSigning(Integer busiType,Integer signingStatus,String startTime,String endTime){
        //参数
        Map<String,Object> params = Maps.newHashMap();
        params.put("busiType",busiType);
        params.put("signingStatus",signingStatus);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        String s = HttpPoolUtils.post(BasicsPlatformUrl.getGetSigning(), null, null,params);
        //转string数据为json
        JSONObject object = JSONObject.parseObject(s);
        if(object == null || object.get("data") == null || !object.getBoolean("success")) {
            return null;
        }
        //转json数据为Javabean
        return JSONObject.toJavaObject(object.getJSONArray("data"),List.class);
    }

    /**
     * 更新二手房资金监管状态 on 2019/5/17 16:11
     *
     * @param signingCode 网签编号
     * @param userId 操作人标识
     * @param userName 操作人姓名
     * @return JSONObject
     */
    public static JSONObject updateShhFunds(String signingCode,String userId,String userName){
        //参数
        Map<String,String> params = Maps.newHashMap();
        params.put("signingCode",signingCode);
        params.put("userId",userId);
        params.put("userName",userName);
        String s = HttpPoolUtils.post(BasicsPlatformUrl.getUpdateShhFunds(), null, params);
        //转string数据为json
        return JSONObject.parseObject(s);
    }
}
